/*
 *  $Id: CreatureGameResolver.cs 1218 2010-11-28 14:03:06Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008-2010 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using Nereal.Serialization;

namespace Ragima.Serialization {
    public sealed class CreatureGameResolver : IKeyResolver<string, Creature> {
        public string ResolveKey(Creature value) {
            if (value == null)
                return string.Empty;
            var playerId = Game.Current.GetPlayerId(value.Owner) ? "1" : "0";
            return playerId + "-" + value.Number.ToString();
        }

        public Creature ResolveValue(string key) {
            var parts = key.Split('-');
            if (parts.Length != 2)
                return null;
            var player = Game.Current.GetPlayerById(parts[0] == "1");
            int number;
            if (int.TryParse(parts[1], out number) && number >= 0 && number < Constants.FieldSize)
                return player.GetCreature(number);
            return null;
        }
    }
}
